package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows
import org.apache.flink.streaming.api.windowing.time.Time

object Demo3SessionWindow {

  def main(args: Array[String]): Unit = {
    //创建flink的运行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val ds: DataStream[String] = env.socketTextStream("master", 7777)


    /**
      *
      * sessionwindow:会话窗口，一次会话结束之后统计
      *
      * ProcessingTimeSessionWindows:处理时间
      * EventTimeSessionWindows：事件时间
      *
      */

    ds.map((_, 1))
      .keyBy(_._1)
      .window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)))
      .sum(1)
      .print()


    env.execute()

  }

}
